package org.jeecg.modules.golf.plan.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.golf.plan.entity.Plan;
import org.jeecg.modules.golf.plan.entity.PlanTypeMapping;
import org.jeecg.modules.golf.plan.mapper.PlanMapper;
import org.jeecg.modules.golf.plan.mapper.PlanTypeMappingMapper;
import org.jeecg.modules.golf.plan.service.IPlanService;
import org.jeecg.modules.golf.plan.service.IPlanTypeMappingService;
import org.jeecg.modules.golf.question.entity.QuestionTypeMapping;
import org.jeecg.modules.golf.question.mapper.QuestionTypeMappingMapper;
import org.jeecg.modules.golf.question.service.IQuestionTypeMappingService;
import org.jeecg.modules.golf.tag.entity.TagMapping;
import org.jeecg.modules.golf.tag.event.TagEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * @Description: 方案
 * @Author: jeecg-boot
 * @Date:   2020-10-20
 * @Version: V1.0
 */
@Service
public class PlanServiceImpl extends ServiceImpl<PlanMapper, Plan> implements IPlanService {

	@Autowired
	private PlanMapper planMapper;
	@Autowired
	private QuestionTypeMappingMapper questionTypeMappingMapper;
	@Autowired
	private PlanTypeMappingMapper planTypeMappingMapper;
	@Autowired
	private ApplicationContext applicationContext;

	@Autowired
	private IQuestionTypeMappingService questionTypeMappingService;

	@Autowired
    private IPlanTypeMappingService planTypeMappingService;








	@Override
	@Transactional(rollbackFor =  Exception.class)
	public void saveMain(Plan plan, List<QuestionTypeMapping> questionTypeMappingList, List<PlanTypeMapping> planTypeMappingList, List<TagMapping> tagMappingList) {
		planMapper.insert(plan);
		questionTypeMappingService.save(questionTypeMappingList,2,plan.getId());
		planTypeMappingService.savePlanTypeMapping(planTypeMappingList,plan.getId());
		applicationContext.publishEvent(new TagEvent(this,plan.getId(),tagMappingList,2));

	}

	@Override
	@Transactional(rollbackFor =  Exception.class)
	public void updateMain(Plan plan,List<QuestionTypeMapping> questionTypeMappingList,List<PlanTypeMapping> planTypeMappingList, List<TagMapping> tagMappingList) {
		planMapper.updateById(plan);

		//1.先删除子表数据
		questionTypeMappingMapper.deleteByMainId(plan.getId());
		planTypeMappingMapper.deleteByMainId(plan.getId());

		//2.子表数据重新插入
		questionTypeMappingService.save(questionTypeMappingList,2,plan.getId());
		planTypeMappingService.savePlanTypeMapping(planTypeMappingList,plan.getId());
		applicationContext.publishEvent(new TagEvent(this,plan.getId(),tagMappingList,2));

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(Long id) {
		questionTypeMappingMapper.deleteByMainId(id);
		planTypeMappingMapper.deleteByMainId(id);
		planMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<Long> idList) {
		for(Long id:idList) {
			questionTypeMappingMapper.deleteByMainId(id);
			planTypeMappingMapper.deleteByMainId(id);
			planMapper.deleteById(id);
		}
	}

}
